﻿using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Tools
{
    public class SyncFbxLocalTransformTool : MonoBehaviour
    {
        public GameObject fbxPrefab;
        public List<GameObject> instances;

        private Dictionary<string, Transform> _fbxTransformMap;

        public void Sync()
        {
#if UNITY_EDITOR
            if (fbxPrefab == null || instances == null)
            {
                return;
            }

            _fbxTransformMap = new Dictionary<string, Transform>();
            var fts = fbxPrefab.GetComponentsInChildren<Transform>(true);
            foreach (var ft in fts)
            {
                if (_fbxTransformMap.ContainsKey(ft.name))
                {
                    EditorUtility.DisplayDialog("错误", $"名称有重复：{ft.name}", "确定");
                    return;
                }

                _fbxTransformMap[ft.name] = ft;
            }

            foreach (var instance in instances)
            {
                var its = instance.GetComponentsInChildren<Transform>(true);
                foreach (var ft in its)
                {
                    if (_fbxTransformMap.ContainsKey(ft.name))
                    {
                        Transform t = _fbxTransformMap[ft.name];
                        ft.localPosition = t.localPosition;
                        ft.localRotation = t.localRotation;
                    }
                }
            }
#endif
            
            
            
        }
        
    }
}